package com.jiai.cloud.utils;


import com.jiai.cloud.base.util.SpringUtil;

import com.jiai.cloud.config.Config;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * Created by Administrator on 2017/3/26.
 */
@Configuration
public class UploadUtil {
     private static final Logger log= LoggerFactory.getLogger(UploadUtil.class);



     public static  String copyFile(MultipartFile file, String outPath)  {
        String newName="";
            String filePath = SpringUtil.getBean(Config.class).getFilePath();
            try {
                newName=UUID.randomUUID() + getExtName(file.getOriginalFilename(), '.');
             /*   if (StringUtils.isEmpty(outPath)) {

                    filePath += File.separator + "other" + File.separator + newName;
                } else {
                    filePath += File.separator + outPath + File.separator + newName;
                }*/
                filePath += File.separator + outPath + File.separator + newName;
            if (file.isEmpty()) {
                return null;
            }
            File dest=new File(filePath);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            log.info("file upload susscess [{}]",filePath);
            return newName;
        }catch (Exception e){
            log.info("file upload error [{}]",e.getMessage());
            return null;
        }
    }

    public static   String getFilePath(String name){

        String filePath = SpringUtil.getBean(Config.class).getFilePath();
        /*if (StringUtils.isEmpty(type)) {

            filePath += File.separator + "other" + File.separator + name;
        } else {
            filePath += File.separator + type + File.separator + name;
        }*/
        filePath += File.separator + File.separator + name;
        return filePath;
    }
    private  static String getExtName(String s, char split) {
        int i = s.indexOf(split);
        int leg = s.length();
        return (i > 0 ? (i + 1) == leg ? " " : s.substring(i, s.length()) : " ");
    }
    //获取传过来的目录
    public static void clearFiles(String workspaceRootPath){
        File file = new File(workspaceRootPath);
        if(file.exists()){
            deleteFile(file);
        }
    }
    //删除目录下的所有文件
    public static void deleteFile(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(int i=0; i<files.length; i++){
                deleteFile(files[i]);
            }
        }
        file.delete();
    }
    //当前文件夹下移动到默认的目录下
    public static void IOCopy(String path,String path1){
        File file = new File(path);
        File file1 = new File(path1);
        byte[] b = new byte[(int) file.length()];
        if(file.isFile()){
            try {
                FileInputStream is= new FileInputStream(file);
                FileOutputStream ps= new FileOutputStream(file1);
                is.read(b);
                ps.write(b);
                is.close();
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }else if(file.isDirectory()){
            String[] list = file.list();
            for(int i=0;i<list.length;i++){
                IOCopy(path+"/"+list[i], path1+"/"+list[i]);
                if (list.length-1==i){ //当前的图片复制完了再删除
                    clearFiles(path);
                }
            }
            clearFiles(path);
        }
    }

}
